<!DOCTYPE html>
<html lang="en">

    <head>
        <meta charset="UTF-8" />
        <link rel="stylesheet" href="styles/style.css">
        <link rel="stylesheet" href="styles/theme/atom-one-dark.css">
        <title>例子50-ES6/7常用语法介绍</title>
    </head>

    <body>
        <pre>
            <code>
                // 例子50

                async function timeout(ms) {
                    await new Promise((resolve) => {
                        setTimeout(resolve, ms);
                    });
                }

                async function asyncPrint(value, ms) {
                    await timeout(ms);
                    console.log(value);
                }
                asyncPrint('hello world', 50);

                //--------------------------------------------------------------------------

                //async函数的多种使用形式
                // 函数声明
                async function foo() {};

                // 函数表达式
                const foo = async function () {};

                // 对象的方法
                let obj = { async foo() {} };
                obj.foo().then(
                    //...
                );

                // Class的方法
                class Storage {
                    constructor() {
                        this.cachePromise = caches.open('avatars');
                    }

                    async getAvatar(name) {
                        const cache = await this.cachePromise;
                        return cache.match(`/avatars/${name}.jpg`);
                    }
                }
                const storage = new Storage();
                storage.getAvatar('jake').then(
                    //...
                );

                // 箭头函数
                const foo = async () => {};
            </code>
        </pre>
        <script src="scripts/libs/highlight.pack.js"></script>
        <script src="scripts/50.js"></script>
        <script>
            hljs.initHighlightingOnLoad();
        </script>
    </body>

</html>